package render.tree
{
	import mx.controls.Button;
	import mx.controls.ButtonBar;
	import mx.controls.Label;
	import mx.controls.treeClasses.TreeItemRenderer;
	import mx.events.ItemClickEvent;

	public class TreeItemRender extends TreeItemRenderer
	{
		private static const DEFAULT_WIDTH:int = 60;
		private static const DEFAULT_HEIGHT:int = 16;
		private static const DEFAULT_SPACE:int = 30;
			
		private var updated:Boolean = false;
		private var buttonBar:ButtonBar;
		private var pageControl:PageControl;
		private var pLabel:Label;
		private var dLabel:Label;
		
		[Embed("/img/next.png")]
		private var next:Class;
		
		[Embed("/img/prev.png")]
		private var prev:Class;
		
		public function TreeItemRender()
		{
			super();
		}
		
		protected function clickHandler(event:ItemClickEvent):void
		{
			if(event.index == 0)
				pageControl.pageUp();
			else if(event.index == 1)
				pageControl.pageDown();
		}
		protected function internalUpdate():void
		{
			if(data is PageControl && (!buttonBar) ){
				pageControl= PageControl(data);
				buttonBar = new ButtonBar();
				pLabel = new Label();		
				var array:Array = new Array({label:"Up",icon:prev},{label:"Down",icon:next});
				
				buttonBar.dataProvider = array;
				buttonBar.width = DEFAULT_WIDTH-20;
				buttonBar.height = DEFAULT_HEIGHT;
				buttonBar.iconField = "icon";
				buttonBar.setStyle("horizontalGap",3);
				buttonBar.addEventListener(ItemClickEvent.ITEM_CLICK,clickHandler);
				pLabel.setStyle("fontWeight","bold");
				pLabel.width = DEFAULT_WIDTH * 2;
				pLabel.height = DEFAULT_HEIGHT;
				
				this.addChild(buttonBar);
				this.addChild(pLabel);
			}
			if(buttonBar){
				if(data is PageControl){
					label.visible = false;
					pLabel.visible = true;
					buttonBar.visible = true;
					buttonBar.x = x + width -DEFAULT_SPACE - buttonBar.width;
					
					Button(buttonBar.getChildAt(0)).enabled = pageControl.pageUpable;
					if(pageControl.pageUpable)
						buttonBar.getChildAt(0).alpha = 1;
					else
						buttonBar.getChildAt(0).alpha = 0.6;
						
					Button(buttonBar.getChildAt(1)).enabled = pageControl.pageDownable;
					if(pageControl.pageDownable)
						buttonBar.getChildAt(1).alpha = 1;
					else
						buttonBar.getChildAt(1).alpha = 0.6;
						
					pLabel.x = buttonBar.x - pLabel.width;
					pLabel.text = pageControl.getPageInfo();
				}
				else{
					label.visible = true;
					pLabel.visible = false;
					buttonBar.visible = false;
				}
			}
			
			if(data is CategoryNode ){
				var edge:String = CategoryNode(data).getEdge();
				if(edge){
					label.htmlText =  transXMLTag(CategoryNode(data).toString()) + "    <I>" + transXMLTag(trim(edge)) + "</I>";
				}	
				else
					label.text =  trim(CategoryNode(data).toString());			
			}
			//
		}
		private static function trim(string:String):String
		{
			var h:int = Math.max(string.lastIndexOf("#"),-1);
			if( h != -1 && h + 1 < string.indexOf(">"))
				return string.substring(h+1,string.indexOf(">"));
			return string;
		}
		private static function transXMLTag(string:String):String
		{
			string = string.replace("&","&amp;");
			string = string.replace(">","&gt;");
			string = string.replace("<","&lt;");
			string = string.replace("'","&apos;");
			string = string.replace('"',"&quot;");
			return string;
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			internalUpdate();
		}
		
	}
}